package com.bjtu.literatureclient.dao.user;


import com.bjtu.literatureclient.domain.base.BaseModel;

import com.bjtu.literatureclient.domain.user.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
public interface IUserDao {

    @Select("<script>SELECT  * FROM usertable WHERE 1=1\n" +
            "<if test=\"userNo!=null and userNo!=''\">\n" +
            "AND UserNo=#{userNo}\n" +
            "</if>\n" +
            "<if test=\"password!=null and password!=''\">\n" +
            "AND Password=#{password}\n" +
            "</if>\n" +
            "<if test=\"userName!=null and userName!=''\">\n" +
            "AND UserName=#{userName}\n" +
            "</if>\n" +
            "<if test=\"userTeamNo!=null and userTeamNo!=''\">\n" +
            "AND UserTeamNo=#{userTeamNo}\n" +
            "</if>\n" +
            "<if test=\"iDNo!=null and iDNo!=''\">\n" +
            "AND IDNo=#{iDNo}\n" +
            "</if>\n" +
            "<if test=\"userPhone!=null and userPhone!=''\">\n" +
            " AND UserPhone=#{userPhone}\n" +
            "</if>" +
            "<if test=\"userEmail!=null and userEmail!=''\">\n" +
            "AND UserEmail=#{userEmail}\n" +
            "</if>\n" +
            "<if test=\"userRole!=null and userRole!=''\">\n" +
            "AND UserRole=#{userRole}\n" +
            "</if>\n" +
            "<if test=\"date!=null and date!=''\">\n" +
            "AND Date=#{date}\n" +
            "</if>\n" +
            "<if test=\"note!=null and note!=''\">\n" +
            "AND Note=#{note}\n" +
            "</if>\n" +
            "<if test=\"dateTime!=null and dateTime!=''\">\n" +
            "AND DateTime=#{dateTime}\n" +
            "</if>\n" +
            "<if test=\"ifuse!=null and ifuse!=''\">\n" +
            "AND IfUse=#{ifuse}\n" +
            "</if>\n" +
            "</script>")
    List<User> query(BaseModel baseModel);

    @Select("select * from usertable where UserEmail = #{userEmail}")
    List<User> selectUserByEmail(String userEmail);

    @Insert("<script>" +
            " INSERT INTO usertable (UserNo, Password, UserName, UserTeamNo, IDNo, UserPhone," +
            " UserEmail, UserRole, Date, Note, DateTime, UserState)" +
            " VALUES (#{userNo}, #{password}, #{userName}, #{userTeamNo}, #{iDNo}, #{userPhone}," +
            " #{userEmail}, #{userRole}, #{date}, #{note}, #{dateTime}, #{userState})" +
            "</script>")
    int insert(User user);

    @Update("update usertable set UserName = #{userName}, IDNo = #{iDNo}, UserPhone = #{userPhone}," +
            " UserEmail = #{userEmail}, Note = #{note}, DateTime = #{dateTime} where UserNo = #{userNo}")
    int update(User user);
    
    /*@Delete("<script>" +
    		"delete from usertable"+
    		" WHERE UserTeamNo=#{userTeamNo}"+
    		"</script>")*/
    int delete(BaseModel baseModel);
    
	User login(User user);
	
	User loginByPhone(User user);
	
	User loginByEmail(User user);

    List<User> checkUserNo(String userNo);

	int checkUserState(String userNo);

	int checkUserStateByPhone(String userNo);

	int checkUserStateByEmail(String userNo);

    @Select("SELECT MIN(UserNo)+1 FROM usertable a WHERE UserTeamNO = #{teamNo} AND NOT EXISTS(SELECT * FROM usertable WHERE UserNo=a.UserNo+1) ")
    String getNextUserNo(String teamNo);

    @Update("UPDATE usertable SET UserState = #{userState} WHERE UserNo = #{userNo} AND UserState = '1'")
    boolean setUserState(@Param("userNo") String userNo, @Param("userState") String userState);

    @Select("SELECT UserNo FROM usertable WHERE UserTeamNo=#{teamNo} ORDER BY UserNo DESC LIMIT 0,1;")
    String selectUserMaxIdByTeamId(@Param("teamNo") String teamNo);

	List<User> getUsersByTeamNo(String teamNo);

}
